﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DCTAccess.Business;
using DCTLib.Common;
using System.Data;

namespace DCTAccess.Business.Administrator
{
    public class AdministratorFilter
    {
        private int _ID = -1;
        private string _userName = string.Empty;
        private string _hashedPassword = string.Empty;
        private string _fullName = string.Empty;
        private string _email = string.Empty;
        private string _mobile = string.Empty;
        private string _address = string.Empty;
        private string _birthday = string.Empty;
        private string _lastLoginDate = string.Empty;
        private int _role = -1;
        private bool _disable = false;
        private string _orderByFullName = string.Empty;

        #region Properties
        public string OrderByFullName
        {
            get { return _orderByFullName; }
            set { _orderByFullName = value; }
        }

        public string Birthday
        {
            get { return _birthday; }
            set { _birthday = value; }
        }
        public string Address
        {
            get { return _address; }
            set { _address = value; }
        }


        public string Mobile
        {
            get { return _mobile; }
            set { _mobile = value; }
        }

        public int Role
        {
            get { return _role; }
            set { _role = value; }
        }

        public int ID
        {
            get { return _ID; }
            set { _ID = value; }
        }

        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }

        public string HashedPassword
        {
            get { return _hashedPassword; }
            set { _hashedPassword = value; }
        }

        public string FullName
        {
            get { return _fullName; }
            set { _fullName = value; }
        }

        public string Email
        {
            get { return _email; }
            set { _email = value; }
        }

        public string LastLoginDate
        {
            get { return _lastLoginDate; }
            set { _lastLoginDate = value; }
        }

        public bool Disable
        {
            get { return _disable; }
            set { _disable = value; }
        }
        #endregion

        public static bool UpdateLastLoginDate(Administrator admin)
        {
            return ExecuteStoredProcedure(admin, "UpdateLastLoginDate");
        }

        private string SelectAllSQL
        {
            get
            {
                StringBuilder sql = new StringBuilder();
                sql.Append("SELECT     AdminID, AdminLoginName, FullName, HashedPassword, Mobile, Address, Email, Birthday = CONVERT(varchar, Birthday, 103), RegDate = CONVERT(varchar, RegDate, 103), LastLoginDate = CONVERT(varchar, LastLoginDate, 103),Role, [Disable] ");
                sql.Append("FROM       Web_Admins ");
                sql.Append("WHERE      [Disable] = 0 ");

                return sql.ToString();
            }
        }

        public DataTable GetAdministrators()
        {
            SQLHelper sqlHelper = new SQLHelper();
            sqlHelper.SwitchCommandType(CommandType.Text);

            StringBuilder sql = new StringBuilder(SelectAllSQL);

            if (ID != -1)
            {
                sql.Append("AND " + IntegerCompareSQLBuilder.GetSQL("AdminID", IntegerCompareTypes.Equals, "@AdminID"));
                sqlHelper.AddParameter("@AdminID", ID);
            }

            if (Role != -1)
            {
                sql.Append("AND " + IntegerCompareSQLBuilder.GetSQL("Role", IntegerCompareTypes.Equals, "@Role"));
                sqlHelper.AddParameter("@Role", Role);
            }

            if (UserName.Trim().Length > 0)
            {
                sql.Append("AND " + StringCompareSQLBuilder.GetSQL("AdminLoginName", StringCompareTypes.Equals, "@AdminLoginName"));
                sqlHelper.AddParameter("@AdminLoginName", UserName);
            }

            if (HashedPassword.Trim().Length > 0)
            {
                sql.Append("AND " + StringCompareSQLBuilder.GetSQL("HashedPassword", StringCompareTypes.Equals, "@HashedPassword"));
                sqlHelper.AddParameter("@HashedPassword", HashedPassword);
            }

            if (FullName.Trim().Length > 0)
            {
                sql.Append("AND " + StringCompareSQLBuilder.GetSQL("FullName", StringCompareTypes.Contains, "@FullName", true));
                sqlHelper.AddParameter("@FullName", FullName.ToLower());
            }

            if (OrderByFullName.Length > 0)
            {
                sql.Append(OrderByFullName);
                return sqlHelper.GetDataTable(sql.ToString());
            }

            return sqlHelper.GetDataTable(sql.ToString() + " ORDER BY AdminLoginName ASC  ");
        }

        private static bool ExecuteStoredProcedure(Administrator admin, string action)
        {
            SQLHelper sqlHelper = new SQLHelper();
            string sProc = "Administrator_spr";
            sqlHelper.AddParameter("@Action", action);
            sqlHelper.AddParameter("@AdminID", admin.ID);
            sqlHelper.AddParameter("@AdminLoginName", admin.UserName);
            sqlHelper.AddParameter("@HashedPassword", admin.HashedPassword);
            sqlHelper.AddParameter("@FullName", admin.FullName);
            sqlHelper.AddParameter("@Mobile", admin.Mobile);
            sqlHelper.AddParameter("@Address", admin.Address);
            sqlHelper.AddParameter("@Email", admin.Email);
            sqlHelper.AddParameter("@Role", admin.Role);
            return sqlHelper.ExecuteSQL(sProc);
        }
    }
}
